package com.gao.weblog.jwt.handler;

import com.gao.weblog.common.enums.ResponseCodeEnum;
import com.gao.weblog.common.utils.Response;
import com.gao.weblog.jwt.exception.UsernameOrPasswordNullException;
import com.gao.weblog.jwt.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class RestAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
      log.warn("AuthenticationException: ", exception);
      if (exception instanceof UsernameOrPasswordNullException){
          ResultUtil.fail(response, Response.fail(exception.getMessage()));
          return;
      }else if (exception instanceof BadCredentialsException){
          ResultUtil.fail(response, Response.fail(ResponseCodeEnum.USERNAME_OR_PWD_ERROR));
          return;
      }
      ResultUtil.fail(response, Response.fail(ResponseCodeEnum.LOGIN_FAIL));
    }
}
